package sandbox.vaadin.skiller.entity;

import com.vaadin.data.Item;
import com.vaadin.data.fieldgroup.FieldGroup;
import com.vaadin.data.fieldgroup.FieldGroup.CommitException;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class SkillEditor extends Window implements Button.ClickListener
{
	private static final long serialVersionUID = 1L;

	private FieldGroup fieldgroup;

	private Button btnSave;
	private Button btnCancel;

	public SkillEditor(Item skillItem)
	{
//		this.skillgroupItem = skillgroupItem;

		TextField tfId = new TextField("Id");
		TextField tfName = new TextField("Name");

		FormLayout formLayout = new FormLayout();
		formLayout.addComponent(tfId);
		formLayout.addComponent(tfName);

		btnSave = new Button("Save", this);
		btnCancel = new Button("Cancel", this);

		HorizontalLayout horizontalLayout = new HorizontalLayout();
		horizontalLayout.addComponent(btnSave);
		horizontalLayout.addComponent(btnCancel);

		VerticalLayout verticalLayout = new VerticalLayout();
		verticalLayout.addComponent(formLayout);
		verticalLayout.addComponent(horizontalLayout);

		fieldgroup = new FieldGroup(skillItem);
		fieldgroup.setBuffered(true);
		fieldgroup.bind(tfId, "id");
		fieldgroup.bind(tfName, "name");

		setCaption("skill editor");
		setContent(verticalLayout);
	}

	@Override
	public void buttonClick(ClickEvent event)
	{
		if (event.getButton() == btnSave)
		{
			try
			{
				fieldgroup.commit();
//				fireEvent(new EditorSavedEvent(this, skillgroupItem));
			}
			catch (CommitException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if (event.getButton() == btnCancel)
		{
			fieldgroup.discard();
		}
		close();
	}
}